import Vue from "vue";
import { logout, getUserPlayList } from "@/api/api_user";
import {getUserFavorites} from "@/api/api_playlist"
import router from "@/router/index";
export default {
  /* music */
  playMusic({ commit }, payload) {
    const { list, id } = payload;
    commit("setMusicList", list);
    commit("setCurrenMusicId", id);
    let index = list.findIndex((item) => item.id == id);
    commit("setCurrenIndex", index);
    commit("setPlayType", "music");
    commit("setPlayState", true);
  },
  logout({ commit, state }) {
    /* 退出登录 */
    if (!state.isLogin) {
      router.push("/login");
      return;
    }
    Vue.prototype
      .$confirm("将要退出登录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(async () => {
        const res = await logout();
        if (res.code !== 200) return;
        Vue.prototype.$message({
          type: "success",
          message: "退出成功",
          duration: 700,
        });
        commit("setIsLogin", false);
        commit("setProfile", null);
        localStorage.removeItem("token");
        router.push("/");
      })
      .catch((err) => {
        console.log(err);
      });
  },
  /* 获取个人歌单 */
  async getMyPlayList({ commit, state }) {
    if (!state.isLogin) return;
    const res = await getUserPlayList();
    if (res.status !== 200) return;
    commit("setMyPlayList", res.data.playList);
  },
  /* 获取收藏歌单 */
  async getcollectPlayList({ commit, state }){
    let uuid = JSON.parse(localStorage.getItem("profile")).data.uuid;
    if (!state.isLogin) return;
    const res = await getUserFavorites(uuid);
    if (res.code !== 200) return;
    commit("setCollectPlayList", res.playLists);
  },
};
